//
//  ValidView.m
//  FadeSDWebImage
//
//  Created by 冷悦 on 16/2/25.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "ValidView.h"
@interface ValidView ()
@property (nonatomic, strong) CAShapeLayer *line1;
@property (nonatomic, strong) CAShapeLayer *line2;
@property (nonatomic, strong) CAShapeLayer *backgroundLayer;
@end

@implementation ValidView
#pragma mark - Initialize
- (instancetype)init{
    if (self = [super init]) {
        self.isShowSymbol = YES;
    }
         
   return self;
}

#pragma mark - Private
/**
 *  计算以一点为圆心，给定角度与半径，在圆上的位置
 *
 *  @param center 圆心
 *  @param angle  给定的角度，使用0-360，而不是3.1415926方式
 *  @param radius 半径
 *
 *  @return 圆上的点
 */
- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint)center
                                 withAngle:(CGFloat)angle
                                 andRadius:(CGFloat)radius{
    CGFloat x = radius * cosf(angle * M_PI / 180);
    CGFloat y = radius * sinf(angle * M_PI / 180);
    
    return CGPointMake(center.x + x, center.y + y);
}

/**
 *  计算❌的点坐标
 *
 *  @param refPoint 参照点
 *  @param radius   半径
 */
- (void)setupIncorrectLineAtRefPoint:(CGPoint)refPoint withRadius:(CGFloat)radius{
    CGFloat lineLength = radius / 1.5;
    CGPoint line1Point1 = [self calcCircleCoordinateWithCenter:refPoint
                                                     withAngle:45
                                                     andRadius:lineLength];
    
    CGPoint line1Point2 = [self calcCircleCoordinateWithCenter:refPoint
                                                     withAngle:225
                                                     andRadius:lineLength];
    
    CGPoint line2Point1 = [self calcCircleCoordinateWithCenter:refPoint
                                                     withAngle:135
                                                     andRadius:lineLength];
    
    CGPoint line2Point2 = [self calcCircleCoordinateWithCenter:refPoint
                                                     withAngle:315
                                                     andRadius:lineLength];
    
    UIBezierPath *line1Path = [UIBezierPath bezierPath];
    [line1Path moveToPoint:line1Point1];
    [line1Path addLineToPoint:line1Point2];
    self.line1.path = line1Path.CGPath;
    
    UIBezierPath *line2Path = [UIBezierPath bezierPath];
    [line2Path moveToPoint:line2Point1];
    [line2Path addLineToPoint:line2Point2];
    self.line2.path = line2Path.CGPath;
}

/**
 *  计算✅的坐标
 *
 *  @param refPoint 参照点
 *  @param radius   半径
 */
- (void)setupCorrectLineAtRefPoint:(CGPoint)refPoint withRadius:(CGFloat)radius{
//    CGFloat centerY = refPoint.y;
//    CGPoint newRefPoint = CGPointMake(refPoint.x - radius * 0.2, centerY + radius / 2.0);
    
    CGPoint line1Point = [self calcCircleCoordinateWithCenter:refPoint
                                                    withAngle:180
                                                    andRadius:radius / 1.8];
    
    CGPoint line2Point = [self calcCircleCoordinateWithCenter:refPoint
                                                    withAngle:315
                                                    andRadius:radius / 1.3];
    
    CGPoint line3Point = [self calcCircleCoordinateWithCenter:refPoint withAngle:90 andRadius:radius / 2];
    
    UIBezierPath *line1Path = [UIBezierPath bezierPath];
    [line1Path moveToPoint:line1Point];
//    [line1Path addLineToPoint:newRefPoint];
    [line1Path addLineToPoint:line3Point];
    self.line1.path = line1Path.CGPath;
    
    UIBezierPath *line2Path = [UIBezierPath bezierPath];
    [line2Path moveToPoint:line3Point];
    [line2Path addLineToPoint:line2Point];
    self.line2.path = line2Path.CGPath;
}

/**
 *  删除所有的CAShapeLayer
 */
- (void)removeCustomLayers{
    [self.line1 removeFromSuperlayer];
    [self.line2 removeFromSuperlayer];
    [self.backgroundLayer removeFromSuperlayer];
}

/**
 *  通过圆心以及半径得到圆形的最大外接矩形
 *
 *  @param refPoint 圆心
 *  @param radius   半径
 *
 *  @return 最大外接矩形
 */
- (CGRect)createRectWithRefPoint:(CGPoint)refPoint withRadius:(CGFloat)radius{
    CGFloat x = refPoint.x - radius;
    CGFloat y = refPoint.y - radius;
    CGFloat widthAndHeight = radius * 2;
    
    return CGRectMake(x, y, widthAndHeight, widthAndHeight);
}

/*
 *  通过圆心以及半径构建所有的CustomLayers
 *
 *  @param refPoint 圆心
 *  @param radius   半径
 */
- (void)setupCustomLayersWithCenter:(CGPoint)refPoint andRadius:(CGFloat)radius{
    [self removeCustomLayers];
    CGRect rect = [self createRectWithRefPoint:refPoint withRadius:radius];
    self.backgroundLayer.path = [UIBezierPath bezierPathWithOvalInRect:rect].CGPath;
    UIColor *color;
    
    if (self.type == ValidViewCorrect) {
        color = self.correctColor == nil ? [UIColor clearColor] : self.correctColor;
        [self setupCorrectLineAtRefPoint:refPoint withRadius:radius];
    } else if (self.type == ValidViewInCorrect) {
        color = self.incorrectColor == nil ? [UIColor clearColor] : self.incorrectColor;
        [self setupIncorrectLineAtRefPoint:refPoint withRadius:radius];
    } else {
        color = self.emptyColor == nil ? [UIColor clearColor] : self.emptyColor;
    }
    
    self.backgroundLayer.fillColor = color.CGColor;
    [self.layer addSublayer:self.backgroundLayer];
    
    if (self.type != ValidViewEmpty) {
        CGFloat lineWidth = self.linewWidth <= 0 ? radius / 5.0 : self.linewWidth;
        UIColor *lineColor = self.lineColor == nil ? [UIColor redColor] : self.lineColor;
        self.line1.lineWidth = lineWidth;
        self.line1.strokeColor = lineColor.CGColor;
        self.line1.fillColor = [UIColor redColor].CGColor;
        [self.layer addSublayer:self.line1];
        
//        if (self.type == ValidViewInCorrect) {
            self.line2.lineWidth = lineWidth;
            self.line2.fillColor = [UIColor redColor].CGColor;
            self.line2.strokeColor = lineColor.CGColor;
            [self.layer addSublayer:self.line2];
            
//        }
        
    }
}

#pragma mark - Override
- (void)drawRect:(CGRect)rect{
    if (self.isShowSymbol == YES) {
        CGFloat width = CGRectGetWidth(rect);
        CGFloat height = CGRectGetHeight(rect);
        CGRect bounds = CGRectMake(0, 0, width, height);
        CGPoint centerPoint = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
        CGFloat radius = MIN(width, height) / 2.0;
        [self setupCustomLayersWithCenter:centerPoint andRadius:radius];
    } else {
        [self removeCustomLayers];
    }
}

#pragma mark - Getters
- (CAShapeLayer *)line1 {
    if (_line1 == nil) {
        _line1 = [CAShapeLayer new];
        _line1.lineJoin = kCALineJoinRound;
        _line1.lineCap = kCALineJoinRound;
    }
    
    return _line1;
}

- (CAShapeLayer *)line2 {
    if (_line2 == nil) {
        _line2 = [CAShapeLayer new];
        _line2.lineJoin = kCALineJoinRound;
        _line2.lineCap = kCALineJoinRound;
    }
    
    return _line2;
}

- (CAShapeLayer *)backgroundLayer {
    if (_backgroundLayer == nil) {
        _backgroundLayer = [CAShapeLayer new];
    }
    
    return _backgroundLayer;
}

#pragma mark - Setters
- (void)setType:(ValidViewType)type{
    _type = type;
    [self setNeedsDisplay];
}

- (void)setIsShowSymbol:(BOOL)isShowSymbol{
    _isShowSymbol = isShowSymbol;
    [self setNeedsDisplay];
}

@end
